<?php

$__FILEPATH__ = dirname(__FILE__)."/";

ob_start();
session_start();

require_once($__FILEPATH__."../func_common.php");
require_once($__FILEPATH__."../class_def.php");
require_once($__FILEPATH__."../conf/info.php");


if (!isset($_SESSION["admin"]))
{
	ob_end_clean();
	ob_get_clean();
	header("HTTP/1.0 403 Forbidden");
	header("Content-Type: text/plain; charset=UTF-8");
	exit(0);
}

$PageArray = array();
$mysqli = GetMysqliInstance();

if(isset($_POST["add"]))
{    
    $time 	= GetSysTime();
    $user 	= $_SESSION["user"];
    $ip 	= GetRequestIP();
    $title 	= $_POST["title"];
    $src 	= $_POST["src"];
    $src 	= strtolower($src);
    
    if(substr($src,0,7)!="http://")
    	$src="http://".$src;

    $strsql = "insert into linklist(time, ip, user, title, src) values(?, ?, ?, ?, ?)";
    $stmt 	= $mysqli->prepare($strsql);
    $stmt->bind_param("sssss", $time, $ip, $user, $title, $src);
    $ret 	= $stmt->execute();
    $stmt->close();

	echo "<script type=\"text/javascript\">alert(\"".$ret ? "添加成功":"添加失败"."\");window.location=\"link.php\";</script>";
}
else if(isset($_POST["delete"]))
{
   $id = intval($_POST["id"]);
   $ret = $mysqli->query("delete from linklist where id='$id'");
   echo "<script type=\"text/javascript\">alert(\"".$ret ? "删除成功" : "删除失败"."\");window.location=\"link.php\";</script>";
}


$result = $mysqli->query("select * from linklist order by time desc", MYSQLI_USE_RESULT);
$PageArray["linklist"] = array();
if ($result)
{
	while($row = $result->fetch_array(MYSQLI_ASSOC))
	{
		$LinkItem = new LinkListItem();
		$LinkItem->id 	 = $row["id"];
		$LinkItem->user  = $row["user"];
		$LinkItem->src 	 = $row["src"];
		$LinkItem->title = $row["title"];
		$LinkItem->ip 	 = $row["ip"];
		$LinkItem->time  = $row["time"];
		$PageArray["linklist"][] = $LinkItem;
	}

	function cmp($a, $b)
	{
		return strlen($a->title) - strlen($b->title);
	}

	usort($PageArray["linklist"], cmp);

	$result->close();
}

$mysqli->close();

?>